<template>
  <!-- $t is vue-i18n global function to translate lang -->
  <div class="app-container">
    <el-row class="mt-4">
      <el-col :span="3">
        <el-menu
          :default-active="activeIndex"
          class="el-menu-vertical-demo"
          @select="handleIndexChange"
        >
          <el-menu-item
            :index="item.key"
            :key="item.key"
            v-for="item in appTypes"
          >
            <i class="el-icon-menu"></i>
            <span slot="title">{{ item.title }}</span>
          </el-menu-item>
        </el-menu>
      </el-col>
      <el-col :span="20">
        <div class="flex flex-row ml-8 scroll-box">
          <app-list
            v-if="loading"
            :index="activeIndex"
            :keyword="searchKeyword"
          ></app-list>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getAppTypes } from "@/api/app";
import AppList from "./app-list.vue";

export default {
  name: "AppManager",
  components: {
    AppList,
  },
  data() {
    return {
      activeIndex: "",
      appTypes: [],
      searchKeyword: "",
      loading: false,
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      getAppTypes().then((res) => {
        this.appTypes = res.data;
        if (this.appTypes.length > 0) this.activeIndex = this.appTypes[0].key;
        this.handleSearch();
      });
    },
    handleIndexChange(index) {
      this.activeIndex = index;
      this.handleSearch();
    },
    handleSearch() {
      //hook refresh app list
      this.loading = false;
      this.$nextTick(() => {
        this.loading = true;
      });
    },
  },
};
</script>
